﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public partial class ServerWin : Form
    {
        private UdpServer udpServer;
        public static ServerWin ins;
        public bool ServerOn = false;
        //窗口初始化
        public ServerWin()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;  //防止新线程调用主线程卡死
            ins = this;
            string Ip = Config.ReadKey("IP");
            txtIP.Text = Ip;
            txtIP.Items.Add(Ip);
            txtIP.Items.Add(Config.ReadKey("IPself"));
            txtPort.Text = Config.ReadKey("Port");
        }

        /// <summary>
        /// 消息显示到窗口列表中，msg消息字符
        /// </summary>
        public void ShowMsg(string msg)
        {
            //dataTime自动加上系统当前时间
            listBox1.Items.Add(DateTime.Now.ToLocalTime().ToString() + ">>" + msg);
            listBox1.TopIndex = listBox1.Items.Count - 1;   //自动跳到最后一行
        }

        public void AddClient(string clientEnd)
        {
            PListBox.Items.Add(clientEnd);
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            udpServer = new UdpServer(txtIP.Text, txtPort.Text);
            udpServer.Init();
            btnOpen.Enabled = false;
        }
    }
}
